﻿package mortal.game.resource
{
    import com.gengine.core.*;
    import com.gengine.resource.*;
    import flash.utils.*;
    import mortal.game.resource.info.*;

    public class GameMapConfig extends Singleton
    {
        private var _mapBg:Dictionary;
        private var _map:Dictionary;
        private var _mapVersion:Dictionary;
        private var _npcMap:Dictionary;
        private static var _instance:GameMapConfig;

        public function GameMapConfig()
        {
            this._mapBg = new Dictionary();
            this._map = new Dictionary();
            this._npcMap = new Dictionary();
            this._mapVersion = new Dictionary();
            this.init();
            return;
        }// end function

        public function getVersionById(param1:int) : String
        {
            return this._mapVersion[param1];
        }// end function

        public function getMapInfo(param1:int) : GMapInfo
        {
            return this._map[param1];
        }// end function

        private function write(param1:Object) : void
        {
            var _loc_2:GMapBgInfo = null;
            var _loc_3:GMapInfo = null;
            var _loc_4:Object = null;
            var _loc_7:Object = null;
            var _loc_5:* = param1.bgMaps;
            var _loc_6:* = param1.maps;
            for each (_loc_4 in _loc_5.bgMap)
            {
                
                _loc_2 = new GMapBgInfo();
                _loc_2.mapName = _loc_4.id;
                _loc_2.mapWidth = _loc_4.mapWidth;
                _loc_2.mapHeight = _loc_4.mapHeight;
                _loc_2.version = _loc_4.version;
                _loc_2.autoMove = _loc_4.autoMove;
                this._mapBg[_loc_2.mapName] = _loc_2;
            }
            for each (_loc_4 in _loc_6.map)
            {
                
                _loc_3 = new GMapInfo();
                _loc_3.id = _loc_4.id;
                _loc_3.name = _loc_4.name;
                _loc_3.hasBossPoint = _loc_4.hasBossPoint;
                _loc_3.version = _loc_4.version;
                _loc_3.des = _loc_4.des;
                _loc_3.mapscene = _loc_4.mapscene;
                _loc_3.enterMusic = _loc_4.enterMusic;
                _loc_3.bgMusic = _loc_4.bgMusic;
                _loc_3.bgInfo = this._mapBg[_loc_4.bgMap];
                if (_loc_4.bgMusicInterval)
                {
                    _loc_3.bgMusicInterval = _loc_4.bgMusicInterval;
                }
                if (_loc_4.sceneEffect)
                {
                    _loc_3.sceneEffect = _loc_4.sceneEffect;
                }
                if (_loc_4.enterEffect)
                {
                    _loc_3.enterEffect = _loc_4.enterEffect;
                }
                this._mapVersion[_loc_3.id] = _loc_3.version;
                _loc_3.copyid = _loc_4.copyid;
                this._map[_loc_3.id] = _loc_3;
                if (_loc_4.hasOwnProperty("npc"))
                {
                    if (_loc_4.npc is Array)
                    {
                        for each (_loc_7 in _loc_4.npc)
                        {
                            
                            this.addNPCToType(_loc_3.id, _loc_7);
                        }
                        continue;
                    }
                    if (_loc_4.npc is Object)
                    {
                        this.addNPCToType(_loc_3.id, _loc_4.npc);
                    }
                }
            }
            return;
        }// end function

        private function addNPCToType(param1:int, param2:Object) : void
        {
            if (param2 == null)
            {
                return;
            }
            var _loc_3:* = this._npcMap[param2.type];
            if (_loc_3 == null)
            {
                var _loc_4:* = [];
                _loc_3 = [];
                this._npcMap[param2.type] = _loc_4;
            }
            _loc_3.push({id:param1, text:param2.npcId});
            return;
        }// end function

        public function getNpcByType(param1:String) : Array
        {
            return this._npcMap[param1];
        }// end function

        public function init() : void
        {
            var _loc_1:* = ConfigManager.instance.getObjectByFileName("gameMap.xml");
            this.write(_loc_1);
            return;
        }// end function

        public static function get instance() : GameMapConfig
        {
            if (_instance == null)
            {
                _instance = new GameMapConfig;
            }
            return _instance;
        }// end function

    }
}
